<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\WxIcon;
use App\Jobs\Icon\UpdateIconImgWHJob;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxIconController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxIcon(), function (Grid $grid) {
            global $__MINI_GLOBAL_TENANT_ID__;
            if($__MINI_GLOBAL_TENANT_ID__ > 0){
                $grid->model()->where('tenant_id', '=', $__MINI_GLOBAL_TENANT_ID__);
            }
            $grid->quickSearch(['id','name'])->placeholder('搜索图标ID，名字...');
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('icon')->display(function ($value){
                return '<i class="mini-icon mini-icon2 '.$value.'"></i>';
            });
            $grid->column('unicode16');

            $grid->column('img')->image('',80);
            $grid->column('desc')->display(function ($v){
                if(!_empty_($this->img) && $this->img_width === null){
                    UpdateIconImgWHJob::dispatch($this->id);
                }
                return $v;
            })->limit(20);
            $grid->column('type')->using(FieldUtils::getUrlTypes());
            $grid->column('target_id');
//            $grid->column('created_at');
            $grid->column('is_admin')->using([0=>'否', 1 =>'是']);
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxIcon(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('icon');
            $show->field('unicode16');

            $show->field('img');
            $show->field('desc');
            $show->field('type');
            $show->field('target_id');
            $show->field('is_admin');

            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxIcon(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->text('icon')->help('进入<a href="https://www.iconfont.cn/" target="_blank">https://www.iconfont.cn/</a>,  点击查看<a href="https://img.mini.minisns.cn/icons/icontip.jpg" target="_blank">使用说明</a>');
            $form->text('unicode16')->help('有些情况下，只能配置unicode（16进制），也非常简单，在iconfont，点击编辑后，复制该值。点击<a href="https://img.mini.minisns.cn/images/unicode16.jpg" target="_blank">查看教程</a>，正常情况下，使用上方的类名，如果上面的不好使，则使用unicode，或者问我');
            $form->image('img')->url('files/uploads')->uniqueName()->autoUpload();
            $form->text('desc');
            $form->radio('type')->options(FieldUtils::getUrlTypes())->default(0)->required();
            $form->text('target_id')->help('<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            $form->radio('is_admin')->options([0=>'否', 1 =>'是'])->default(0);

            $form->display('created_at');
            $form->display('updated_at');

            global $__MINI_GLOBAL_TENANT_ID__;
            if($__MINI_GLOBAL_TENANT_ID__ > 0){
                $form->text('tenant_id', '本站分应用id')->help('忽略这个配置')->default($__MINI_GLOBAL_TENANT_ID__);
            }

            $form->saving(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                $form->tenant_id = $__MINI_GLOBAL_TENANT_ID__;
                if(_empty_($form->icon) && _empty_($form->unicode16) && _empty_($form->img)){
                    return $form->response()->error('icon,unicode16,img三者必须配置其一');
                }
            });

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
